﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Infrastructure.Base.InDatabase;
using Domain.Model.Entidades.Vehiculos;
using Domain.Model.Vehiculos;

namespace InfraccionesWeb.Formularios
{
    public partial class Vehiculos : System.Web.UI.Page
    {
        //referencia a los servicios de agente
        VehiculoService vehiculoService = new VehiculoService(new BVehiculoRepository());

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                llenarGrilla();
            }

        }
        private void llenarGrilla()
        {
            List<Vehiculo> lista = vehiculoService.All();
            gdwVehiculos.DataSource = lista;
            gdwVehiculos.DataBind();
        }

        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            String vin = txtNuevoVin.Text;
            String placa = txtNuevoPlaca.Text;
            String licenciaVehiculo = txtNuevoLicencia.Text;
            String tipo = txtNuevoTipo.Text;
            String servicio = txtNuevoServicio.Text;

            vehiculoService.CreateVehiculo(vin, placa, licenciaVehiculo, tipo, servicio);

            txtNuevoVin.Text = txtNuevoPlaca.Text = txtNuevoLicencia.Text = txtNuevoTipo.Text=txtNuevoServicio.Text="";
            llenarGrilla();
        }


        protected void gdwVehiculos_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdwVehiculos.EditIndex = -1;
            llenarGrilla();
        }

        protected void gdwVehiculos_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            String id = ((Label)gdwVehiculos.Rows[e.RowIndex].FindControl("lblIdentity")).Text;

            vehiculoService.RemoveVehiculo(new VehiculoId(id));
            gdwVehiculos.EditIndex = -1;
            llenarGrilla();
        }

        protected void gdwVehiculos_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdwVehiculos.EditIndex = e.NewEditIndex;
            llenarGrilla();
        }

        protected void gdwVehiculos_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            String id = ((TextBox)gdwVehiculos.Rows[e.RowIndex].FindControl("txtIdentity")).Text;
            String vin = ((TextBox)gdwVehiculos.Rows[e.RowIndex].FindControl("txtVin")).Text;
            String placa = ((TextBox)gdwVehiculos.Rows[e.RowIndex].FindControl("txtPlaca")).Text;
            String licencia = ((TextBox)gdwVehiculos.Rows[e.RowIndex].FindControl("txtLicencia")).Text;
            String tipo = ((TextBox)gdwVehiculos.Rows[e.RowIndex].FindControl("txtTipo")).Text;
            String servicio = ((TextBox)gdwVehiculos.Rows[e.RowIndex].FindControl("txtServicio")).Text;


            vehiculoService.EditVehiculo(new VehiculoId(id),vin,placa, licencia, tipo, servicio);

            gdwVehiculos.EditIndex = -1;
            llenarGrilla();
        }



    }
}